﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
	public class templatesOutput : Api_BaseOutput
	{
		public templatesResults result { get; set; }
		public templatesParms inputParms { get; private set; }
		// --------------------------------------------------------
		public templatesOutput( templatesInput input )
			: base( input )
		{
			inputParms = input.parms;
			result = new templatesResults();
		}
		// --------------------------------------------------- tostring
		public override string ToString()
		{
			return "Not implemented";
		}

		#region ================================================================== format setup

		// ---- serial
		internal void Format( string api_Request, string api_Response )
		{
			this.api_Request = api_Request;
			this.api_Response = api_Response;
			// ----------------------------------
			if ( this.api_OutputType == EnumValues.OutputType.JSON )
				outputJSON();
			else if ( this.api_OutputType == EnumValues.OutputType.XML )
				outputXML();
		}

		// ---- xmlrpc
		internal void Format( x_templates rpcResult )
		{
			this.api_Request = EnumValues.NotSupplied;
			this.api_Response = EnumValues.NotSupplied;
			// ------------------------
			this.result = FormatResults( rpcResult );
		}

		#endregion

		#region  ===================================================================== output result

		// ---------------------------------------------------------- outputXML
		private void outputXML()
		{
			XElement xml = XElement.Parse( this.api_Response );
			Api_Error error = apiSerial.GetXmlOutputError( xml );
			if ( error != null )
			{
				this.api_ErrorMessages.Add( error );
				return;
			}
			// ------------------------------------ get values
			try
			{
				templatesResults results = new templatesResults();
				//
				XElement user = xml.Element( "user" );
				XElement gallery = xml.Element( "gallery" );
				XElement basic = xml.Element( "base" );
				//------------------------------------------------ user templates
				if ( user != null && user.HasElements )
				{
					IEnumerable<XElement> temp_list = user.Elements( "struct" );
					foreach ( XElement item in temp_list )
					{
						templatesResults.template entry = fmtTemplate( item );
						results.user.Add( entry );
					}
				}
				//------------------------------------------------ gallery templates
				if ( gallery != null && gallery.HasElements )
				{
					IEnumerable<XElement> temp_list = gallery.Elements( "struct" );
					foreach ( XElement item in temp_list )
					{
						templatesResults.template entry = fmtTemplate( item );
						results.gallery.Add( entry );
					}
				}
				//------------------------------------------------ basic templates
				if ( basic != null && basic.HasElements )
				{
					IEnumerable<XElement> temp_list = basic.Elements( "struct" );
					foreach ( XElement item in temp_list )
					{
						templatesResults.template entry = fmtTemplate( item );
						results.basic.Add( entry );
					}
				}
				//
				this.result = results;
			}
			// ----------------------------- error
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}

		// ---------------------------------------------------------- outputJSON
		private void outputJSON()
		{
			JavaScriptSerializer jss = new JavaScriptSerializer();

			try
			{
				if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
				{
					Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
					this.api_ErrorMessages.Add( err );
				}
				else
				{
					x_templates jsonResult = jss.Deserialize<x_templates>( this.api_Response );
					this.result = FormatResults( jsonResult );
				}
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}
		// ---------------------------------------------------------- FormatResults
		private templatesResults FormatResults( x_templates x_results )
		{
			templatesResults results = new templatesResults();
			//
			if ( x_results.user != null && x_results.user.Length > 0 )
			{
				foreach ( var entry in x_results.user )
				{
					results.user.Add( fmtTemplate( entry ) );
				}
			}
			if ( x_results.gallery != null && x_results.gallery.Length > 0 )
			{
				foreach ( var entry in x_results.gallery )
				{
					results.gallery.Add( fmtTemplate( entry ) );
				}
			}
			if ( x_results.@base != null && x_results.@base.Length > 0 )
			{
				foreach ( var entry in x_results.@base )
				{
					results.basic.Add( fmtTemplate( entry ) );
				}
			}
			// 
			return results;
		}
		//
		private templatesResults.template fmtTemplate( x_templates.x_template entry )
		{
			DateTime dt;
			templatesResults.template temp = new templatesResults.template();
			temp.id = entry.id;
			temp.name = entry.name;
			temp.category = entry.category ?? "";
			temp.layout = entry.layout ?? "";
			temp.preview_image = entry.preview_image ?? "";
			DateTime.TryParse( entry.date_created, out dt );
			temp.date_created = dt;
			temp.edit_source = entry.edit_source;
			temp.active = ( entry.active.ToLower() == "y" ) ? true : false;
			//
			return temp;
		}
		private templatesResults.template fmtTemplate( XElement item )
		{
			double dbl; DateTime dt; bool bl;
			//
			templatesResults.template entry = new templatesResults.template();
			double.TryParse( item.Element( "id" ).Value, out dbl );
			entry.id = dbl;
			entry.name = item.Element( "name" ).Value ?? "";
			entry.category = item.Element( "category" ).Value ?? "";
			entry.layout = item.Element( "layout" ).Value ?? "";
			DateTime.TryParse( item.Element( "date_created" ).Value, out dt );
			entry.date_created = dt;
			entry.active = ( item.Element( "active" ).Value.ToLower() == "y" ) ? true : false;
			bool.TryParse( item.Element( "edit_source" ).Value, out bl );
			entry.edit_source = bl;
			entry.preview_image = item.Element( "preview_image" ).Value ?? "";
			//
			return entry;
		}

		#endregion
	}
}
